package org.itboys.core.controller.front.pay;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.dto.pay.SundayPayDto;
import org.itboys.core.service.mysql.front.SundayFrontPayService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.pay.alipay.AlipayWapResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: jiaqilei
 * @Date: 19:34 2018/11/28
 * @Discription: 支付
 */
@Controller
@RequestMapping("/sunday/mobile/pay")
@Api(value = "pay", description = "支付")
public class SundayMobilePayController extends BaseController {
    @Autowired
    private SundayFrontPayService payService;

    /**
     * 预支付
     *
     * @param request
     * @param response
     * @param recordIds
     */
    @RequestMapping(value = "/prepay", method = {RequestMethod.POST})
    @ApiOperation(value = "预支付 请先创建充值记录后，再调用此接口",notes = "创建充值记录接口在wallet模块下")
    public void prepay(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "钱包记录ID集合") @RequestParam(value = "recordIds", required = true) List<Long> recordIds
    ) {
        SundayPayDto result = payService.preparePay(recordIds);
        ResponseMessage.success(result);
    }

    /**
     * 支付宝支付回调接口
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/callback/alipay", method = {RequestMethod.POST})
    @ApiOperation(value = "支付宝支付回调接口。由支付宝回调，移动端忽略。")
    public void callback_alipay(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        AlipayWapResult alipayWapResult = payService.callback_alipay(request);
        if (alipayWapResult.isVerify_result()) {
            AjaxUtils.renderJson(response, "success");
        } else {
            AjaxUtils.renderJson(response, "fail");
        }
    }
}
